package main

import (
	"fmt"
	"gitee.com/hufengjiu/go-zinx/ziface"
	"gitee.com/hufengjiu/go-zinx/znet"
	"log"
)

type PingRouter struct {
	znet.Router
}

func (router *PingRouter) PreHandle(r ziface.IRequest) {
	log.Println("Call Router Pre")
	_, err := r.GetConnection().GetTcpConn().Write([]byte("before ping ... \n"))
	if err != nil {
		log.Println("pre ping error", err)
	}
}

func (router *PingRouter) Handle(r ziface.IRequest) {
	log.Println("Call Router ing")
	_, err := r.GetConnection().GetTcpConn().Write([]byte("ping ping ... \n"))
	_, err = r.GetConnection().GetTcpConn().Write([]byte(fmt.Sprintf("echo%s..", r.GetData())))
	if err != nil {
		log.Println("ing ping error", err)
	}
}

func (router *PingRouter) PostHandle(r ziface.IRequest) {
	log.Println("Call Router after")
	_, err := r.GetConnection().GetTcpConn().Write([]byte("after ping ... \n"))
	if err != nil {
		log.Println("after ping error", err)
	}
}

func main() {
	s := znet.NewServer("zinxV1", "0.0.0.0", 9999)
	s.AddRouter(&PingRouter{})
	s.Serve()
}
